
Imports System.IO

Module randomselect

    Sub Main(ByVal args() As String)
        Dim reader As TextReader = Nothing
        Dim writer As TextWriter = Nothing
        Dim input As String = Nothing
        Dim percent As Int32

        If args.Length < 3 Then
            raiseError("argument count error, rate input output, " + _
                        "if input or output is '/-', use console", errorHandle.errorType.critical)
        Else
            If strsame(args(1), "/-") Then
                reader = Console.In
            Else
                reader = New StreamReader(New FileStream(args(1), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            End If
            If strsame(args(2), "/-") Then
                writer = Console.Out
            Else
                writer = New StreamWriter(New FileStream(args(2), FileMode.Create, FileAccess.Write, FileShare.Read))
            End If
            assert(Not reader Is Nothing, "file no found, " + args(1))
            assert(Not writer Is Nothing, "file no found, " + args(2))
            percent = Convert.ToInt32(args(0))

            input = reader.ReadLine
            While Not input Is Nothing
                kickNewlineChar(input)

                If (rnd(0, 100000, True)) Mod 100 < percent Then
                    writer.WriteLine(input)
                End If
                input = reader.ReadLine
            End While

            closestream(reader)
            closestream(writer)
        End If
    End Sub

End Module
